package erfgame.core.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;


import erfgame.core.util.ArrayUtils;
import erfgame.core.world.terrain.TerrainUtils;

public class UpdateSurfacesTester {
	
	@Test
	public void testEmptyUpdate() {
		int[] temp = new int[256];
		int[] surfacesXY = new int[0];
		byte[] terrainXY = {1,0,0,1};
		int[] result = TerrainUtils.updateSurfaces(temp, surfacesXY, terrainXY, 0, terrainXY.length-1);
		assertNotNull( result );
		assertTrue(ArrayUtils.equals( result, new int[]{0,1,3,4} ) );
	}
	
	@Test
	public void testAppendUpdate() {
		int[] temp = new int[256];
		int[] surfacesXY = new int[]{2,3};
		byte[] terrainXY = {1,0,0,1};
		int[] result = TerrainUtils.updateSurfaces(temp, surfacesXY, terrainXY, 3, 3+terrainXY.length-1);
		assertNotNull( result );
		assertTrue(ArrayUtils.equals( result, new int[]{2,4,6,7} ) );
	}
	
	@Test
	public void testPrependUpdate() {
		int[] temp = new int[256];
		int[] surfacesXY = new int[]{5,7};
		byte[] terrainXY = {1,0,0,1};
		int startz = 0;
		int[] result = TerrainUtils.updateSurfaces(temp, surfacesXY, terrainXY, startz, startz+terrainXY.length-1);
		assertNotNull( result );
		assertTrue(ArrayUtils.equals( result, new int[]{0,1,3,4,5,7} ) );
	}
	
	@Test
	public void testPrependUpdate2() {
		int[] temp = new int[256];
		int[] surfacesXY = new int[]{10,12};
		byte[] terrainXY = {0,0,0,1,0,0};
		int startz = 0;
		int[] result = TerrainUtils.updateSurfaces(temp, surfacesXY, terrainXY, startz, startz+terrainXY.length-1);
		assertNotNull( result );
		assertTrue(ArrayUtils.equals( result, new int[]{3,4,10,12} ) );
	}

	@Test
	public void testOverlap() {
		int[] temp = new int[256];
		int[] surfacesXY = new int[]{4,12};
		byte[] terrainXY = {0,0,0,1,1,1};
		int startz = 0;
		int[] result = TerrainUtils.updateSurfaces(temp, surfacesXY, terrainXY, startz, startz+terrainXY.length-1);
		assertNotNull( result );
		assertTrue(ArrayUtils.equals( result, new int[]{3,12} ) );
	}

	@Test
	public void testInsert() {
		int[] temp = new int[256];
		int[] surfacesXY = new int[]{0, 2, 10,12};
		byte[] terrainXY = {1,0,0,1,0};
		int startz = 4;
		int[] result = TerrainUtils.updateSurfaces(temp, surfacesXY, terrainXY, startz, startz+terrainXY.length-1);
		assertNotNull( result );
		assertTrue(ArrayUtils.equals( result, new int[]{0,2,4,5,7,8,10,12} ) );
	}

	@Test
	public void testWipe() {
		int[] temp = new int[256];
		int[] surfacesXY = new int[]{0, 10};
		byte[] terrainXY = {0,0,0,0,0};
		int startz = 2;
		int[] result = TerrainUtils.updateSurfaces(temp, surfacesXY, terrainXY, startz, startz+terrainXY.length-1);
		assertNotNull( result );
		assertTrue(ArrayUtils.equals( result, new int[]{0,2,7,10} ) );
	}
	
}
